package com.lry.basic.thread;

import com.alibaba.ttl.TransmittableThreadLocal;
import com.alibaba.ttl.TtlRunnable;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class TestAliThreadLocal {

    //测试子线程取出父线程设置的变量
    @Test
    public void t(){
        TransmittableThreadLocal<String> context = new TransmittableThreadLocal<>();
        context.set("parent");

        new Thread(()->{
            while(true){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("子线程："+context.get());
            }
        }).start();

        //主线程不停修改
        while(true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            context.set("s");
            System.out.println("主线程："+context.get());
        }
    }

    @Test
    public void t4(){
        ThreadLocal<Map<Integer,Integer>> context = new InheritableThreadLocal<>();
        Map<Integer,Integer> map = new HashMap<>();
        map.put(1,1);
        context.set(map);

        new Thread(()->{
            while(true){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("子线程："+context.get());
            }
        }).start();

        //主线程不停修改
        while(true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            context.get().put(1,2);
            System.out.println("主线程："+context.get());
        }
    }

    @Test
    public void t3(){
        TransmittableThreadLocal<Map<Integer,Integer>> context = new TransmittableThreadLocal<>();
        Map<Integer,Integer> map = new HashMap<>();
        map.put(1,1);
        context.set(map);

        new Thread(()->{
            while(true){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("子线程："+context.get());
            }
        }).start();

        //主线程不停修改
        while(true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            context.get().put(1,2);
            System.out.println("主线程："+context.get());
        }
    }

    //线程池
    @Test
    public void t2() throws InterruptedException {
        ExecutorService executor = Executors.newFixedThreadPool(2);
        TransmittableThreadLocal<String> context = new TransmittableThreadLocal<>();

        context.set("parent");

        executor.submit(TtlRunnable.get(()->{
            System.out.println("task:"+context.get());
        }));

    }


}
